package pers.mingshan.thread.thread;

import java.util.Random;

public class ThreadLocalTest {
private static ThreadLocal<Integer> local = new ThreadLocal<Integer>();

    public static void main(String[] args) {
        for(int i = 0;i < 2; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int data = new Random().nextInt();
                    System.out.println("ThreadName -" + Thread.currentThread().getName() + data);
                    local.set(data);
                    new A().get();
                    new B().get();
                }
            }).start();
        }
    }

    static class A {
        public void get() {
            System.out.println("A  module -"+Thread.currentThread().getName() + local.get());
        }
    }

    static class B {
        public void get() {
            System.out.println("B  module -"+Thread.currentThread().getName() + local.get());
        }
    }

    static class Person {
        private static ThreadLocal<Person> map = new ThreadLocal<Person>();
        private static Person instance = null;
        private Person() {}
        public static Person getInstance() {
            instance = map.get();
            if (instance == null) {
                instance = new Person();
                map.set(instance);
            }

            return instance;
        }

        private String name;
        private int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}
